package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class MotionReactor_AccSimpleBounce
	{
		//VARIABLES INTERNAS SETEABLES OBLIGATORIAS.
		
		public var clip:MovieClip;	
		public var listener:Object;
		
		//VARIABLES INTERNAS SETEABLES OPCIONALES.
			
			var accFactor:Number = 1;	
			var elasticFactor:Number = 0.5;
			var frictionFactor:Number = 0.75;
			
		//VARIABLES INTERNAS.
		
			var initW:Number;
			var initH:Number;
			var aW:Number = 0;
			var aH:Number = 0;
			var vW:Number = 0;
			var vH:Number = 0;
		
		//CONSTRUCTOR.
		public function MotionReactor_AccSimpleBounce()
		{
			
		}
		
		//INIT.
		public function init():void
		{
			//REGISTRA LOS VALORES INICIALES DE ANCHO Y ALTO.
			initW = clip.width;
			initH = clip.height;
			
			//INICIA EL ENTERFRAME.
			clip.addEventListener(Event.ENTER_FRAME, enterFrameHandlerAccSimpleBounce);
		}
		
		//ENTERFRAME.
		private function enterFrameHandlerAccSimpleBounce(evt:Event):void
		{
			//LEE LA ACELERACION DE POSICION DEL OBJETO.
			var fW:Number = Math.abs(listener.aX);
			var fH:Number = Math.abs(listener.aY);
			fW = Math.abs(fW) > 0.01 ? fW : 0;
			fH = Math.abs(fH) > 0.01 ? fH : 0;
				
			//CALCULA LOS OFFSETS DE ANCHO Y ALTO.
			var dW:Number = -(clip.width/2 - initW/2);
			var dH:Number = -(clip.height/2 - initH/2);
			
			//AFECTA dW Y dH.
			dW -= fW*accFactor;
			dH -= fH*accFactor;
			
			//FUERZA ELASTICA.
			vW += dW*elasticFactor;
			vW -= dH*elasticFactor;
			vH += dH*elasticFactor;
			vH -= dW*elasticFactor;
			
			//FRICCION.
			vW *= frictionFactor;
			vH *= frictionFactor;
				
			//APLICA vW y vH.
			clip.width += vW;
			clip.height += vH;
		}
	}
}